wifi.setmode(wifi.STATION)
wifi.sta.config({ssid="admin123", pwd="12345678"})
wifi.sta.connect()

led0_pin = 0; led4_pin = 4;

gpio.mode(led0_pin, gpio.OUTPUT);
gpio.mode(led4_pin, gpio.OUTPUT);


function start_udpserver()
    udpSocket = net.createUDPSocket()
    udpSocket:listen(5000)
    udpSocket:on("receive" , function(s, data, port, ip)
        print(string.format("received '%s' from %s:%d", data, ip, port))
        s:send(port, ip, data .. " OK ")
        if data == "on" then
            gpio.write(led0_pin, gpio.LOW);
            gpio.write(led4_pin, gpio.LOW);
        elseif data == "off" then
            gpio.write(led0_pin, gpio.HIGH);
            gpio.write(led4_pin, gpio.HIGH);
        end
    end)
    port, ip = udpSocket:getaddr()
    print(string.format("local UDP socket address / port: %s:%d", ip, port))
end

mytmr = tmr.create()
mytmr:alarm(500, tmr.ALARM_AUTO, function ()
    wifi_ip  = wifi.sta.getip()
    if wifi_ip ~= nil then
        print(wifi_ip)
        mytmr:stop()
        start_udpserver()
    end
    print("----getip---")
end)
